﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;
using System.Text.RegularExpressions;

namespace mamnontuoitho7.module
{
    public partial class ParentConversationDetails : System.Web.UI.Page
    {
        protected General _general = new General();
        private Conversation _conversation = new Conversation();
        protected Message _message = new Message();
        protected DataTable _dtConversation, _dtLink, _dtMessage;
        private Member _member = new Member();
        protected double rate;

        
     //   string _conversationID = "";
        protected int _classCount = 0;
        protected int _pageflag;
        protected int _currentbar = 1;


        protected int _totalPages;// tổng số trang chứa comment
        protected int _totalMessageNumber;//tong số comment cua bai viet này
        protected int _totalBars;// tổng số bar

        protected int _beginPage;// dung bên trang giao diện
        protected int _endPage;// dung bên trang giao diện

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["memberID"].ToString() != "" && Session["role"].ToString() == "4")
            {
                if (Request.QueryString["ConversationID"] != null)
                {
                    string temp = Request.QueryString["ConversationID"].ToString();
                    Match match = Regex.Match(temp, @"([0-9])$", RegexOptions.IgnoreCase);

                    if (temp.Length <= 4 && match.Success)
                    {
                        bool check = false;
                        try
                        {
                            check = _conversation.checkConversationOwnerForParent(Session["memberID"].ToString(), temp);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                            Response.Redirect(ConstantClass._errorLoginPage);
                        }

                        if (check)
                        {
                            hid_ConversationID.Value = temp;
                        }
                        else
                        {
                            Response.Redirect("Home.aspx");
                        }
                    }
                    else
                    {
                        Response.Redirect("Home.aspx");
                    }

                    try
                    {
                        if (!IsPostBack)
                        {
                            // load drop down list article type
                            DataTable dtParentClass = _member.GetClassByParent(Session["memberID"].ToString());
                            _classCount = dtParentClass.Rows.Count;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        Response.Redirect(ConstantClass._errorLoginPage);
                    }

                }

                try
                {
                    if (Request.Form["txtUpdateComment"] != "" && Request.Form["hidMessageID"] != null && Request.Form["hidStatus"] == "1")
                    {
                        string _messageID = Request.Form["hidMessageID"].ToString();
                        string _content = Request.Form["txtUpdateComment"].ToString();
                        _message.ContentMessageUpdate(_messageID, _content);
                    }

                    _dtConversation = _conversation.GetConversationByID(hid_ConversationID.Value);
                    ////////////////////////start paging
                    _totalMessageNumber = _message.TotalMessageOfOneConversation(hid_ConversationID.Value).GetValueOrDefault();
                    _totalPages = _general.Divide(_totalMessageNumber, ConstantClass._pageStepMessage);
                    _totalBars = _general.Divide(_totalPages, ConstantClass._barStepMessage);


                    if (Request.QueryString["page"] != null)// khi nhận giá trị biến ["page"] trên link khi bấm vào ô số
                    {
                        string temp2 = Request.QueryString["page"].ToString();
                        Match match2 = Regex.Match(temp2, @"([0-9])$", RegexOptions.IgnoreCase);

                        if (temp2.Length <= 4 && match2.Success)
                        {
                            _pageflag = Convert.ToInt16(temp2);// trang hiện hành
                            if (_pageflag <= _totalPages)
                            {
                                _currentbar = _general.Divide(_pageflag, ConstantClass._barStepMessage);
                                Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                            }
                            else
                            {
                                _pageflag = 1; _currentbar = 1;
                                Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                            }
                        }


                    }
                    if (Request.QueryString["page"] == null && Request.QueryString["a"] == null)// khi mới vào trang
                    {
                        if (_totalMessageNumber == 0)
                        {
                            _currentbar = 1;
                            _pageflag = 1;
                            Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                        }
                        else
                        {
                            _currentbar = _totalBars;
                            Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                            _pageflag = _totalPages;
                        }
                    }

                    if (Request.QueryString["a"] != null && Request.QueryString["a"] == "1") // khi bấm vào ô >> (next)
                    {
                        _currentbar = Int16.Parse(Request.Cookies["CurrentBar"].Value);
                        if (_currentbar < _totalBars)
                        {
                            _currentbar += 1;
                            Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                        }
                        _pageflag = (_currentbar - 1) * ConstantClass._barStepMessage + 1;
                    }

                    if (Request.QueryString["a"] != null && Request.QueryString["a"] == "-1") // khi bấm vào ô << (previous)
                    {
                        _currentbar = Int16.Parse(Request.Cookies["CurrentBar"].Value);
                        if (_currentbar > 1)
                        {
                            _currentbar -= 1;
                            Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                        }
                        _pageflag = (_currentbar - 1) * ConstantClass._barStepMessage + 1;
                    }


                    _dtMessage = _message.GetMessagePaged(_pageflag, ConstantClass._pageStepMessage, hid_ConversationID.Value);
                    _beginPage = (_currentbar - 1) * ConstantClass._barStepMessage + 1;
                    _endPage = _currentbar * ConstantClass._barStepMessage;

                    if (_endPage > _totalPages)
                    {
                        _endPage = _totalPages;
                    }

                    ////////////////////////end paging
                    _conversation.ChangeStatusOwner(hid_ConversationID.Value, "1");

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorLoginPage);
                }
            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);

            }

            Session["Common_articleTypeID"] = "1";

        }

        protected void btnMessage_Click(object sender, EventArgs e)
        {
            try
            {
                _message.MessageCreate(hid_ConversationID.Value, Session["memberID"].ToString(), txtMessage.Text.Trim(), "1", "0");
                txtMessage.Text = "";
                _totalMessageNumber = _message.TotalMessageOfOneConversation(hid_ConversationID.Value).GetValueOrDefault();
                _totalPages = _general.Divide(_totalMessageNumber, ConstantClass._pageStepMessage);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorLoginPage);
            }

            Response.Redirect("ParentConversationDetails.aspx?ConversationID=" + hid_ConversationID.Value + "&page=" + _totalPages);
        }
    }
}